<?php 
/******* 
 * Stephen Hitchner (steve@number41media.com) 
 * db_mysql.class 
 *
 * Created:  [07/2007] 
 * Revised:  [07/2007]
 *
 * Updated for PHP5
 *
 * To view errors simply define a LOG_FILE eg 
 * define("LOG_FILE", "/path/to/log"); 
 * using the command 
 * tail -f /path/to/log 
 * You can view the errors as they happen 
 *
 * To set log level define LOG_LEVEL
 * DEBUG, INFO, WARN, ERROR, FATAL
 */ 
define('DEBUG',	4);
define('INFO',	3);
define('WARN',	2);
define('ERROR',	1);
define('FATAL',	0);

/******************************************************* 
 * Log Class 
 *******************************************************/ 
class Log {

	//----------------------------------------------------- 
	public static function dump($var) {
		ob_start();
		var_dump($var);
		$str = ob_get_contents();
		ob_end_clean();
		Log::append(DEBUG, $str);
	}
	//----------------------------------------------------- 
	public static function debug($str) {
		Log::append(DEBUG, $str);
	}
	//----------------------------------------------------- 
	public static function isDebug() {
		return (DEBUG == LOG_LEVEL);
	}
	//----------------------------------------------------- 
	public static function info($str) {
		Log::append(INFO, $str);
	}
	//----------------------------------------------------- 
	public static function isInfo() {
		return (INFO == LOG_LEVEL);
	}
	//----------------------------------------------------- 
	public static function warn($str) {
		Log::append(WARN, $str);
	}
	//----------------------------------------------------- 
	public static function isWarn() {
		return (WARN == LOG_LEVEL);
	}
	//----------------------------------------------------- 
	public static function error($str) {
		Log::append(ERROR, $str);
	}
	//----------------------------------------------------- 
	public static function isError() {
		return (ERROR == LOG_LEVEL);
	}
	//----------------------------------------------------- 
	public static function fatal($str) {
		Log::append(FATAL, $str);
	}
	//----------------------------------------------------- 
	public static function isFatalDebug() {
		return (FATAL == LOG_LEVEL);
	}
	//-----------------------------------------------------
	private static function append($level, $message) { 
		global $_SERVER;

		if ( defined('LOG_FILE') && $level <= LOG_LEVEL) { 
			$error = date('dMY H:i:s') . 
				 ' - ' .
				 Log::level2String($level) . 
				 ' - ' .
				 $_SERVER['PHP_SELF'] .
				 ' ' .
				 preg_replace('/\s{2,}/',' ',$message); 
      			@error_log($error . "\r\n", 3, LOG_FILE); 
    		} 
	}

	private static function level2String($level) {
		switch($level) {
		case FATAL: return 'FATAL';
		case ERROR: return 'ERROR';
		case WARN: return 'WARN';
		case INFO: return 'INFO';
		case DEBUG: return 'DEBUG';
		default: return 'UNKNOWN';
		}
	}
}
 
?>
